package com.xmlit.project.engine.struct.impl;

import java.util.List;
//import java.util.regex.Pattern;

import com.xmlit.project.engine.struct.Struct;
import com.xmlit.project.engine.struct.StructSequence;

public class StructSequenceImpl extends StructImpl implements StructSequence {

	public StructSequenceImpl() {super();};
	public StructSequenceImpl(String name) {
		super(name);
	}

	String delimiter = null;
	String delimiterRegex = null;
	String lookahead = null;

	boolean isNegative = false;


	public String getDelimiter() {
		return delimiter;
	}

	
	

	public StructSequenceImpl setDelimiter(String del) {
		this.delimiter = del;
		return this;
	}



	public String calcRegex(CalcContext context, String delimiter) {
		if (this.regex == null) {
			this.indexWithoutOccur = context.calcIndex;
			handleOccurIndex(context, delimiter);
			this.regexIndex = context.calcIndex;
			context.calcIndex++;


			/*if (!(this.getMinOccurrences() == 1 && this.getMaxOccurrences() == 1)) {
				context.calcIndex++;
				if (getParentDelimiter() != null) {
					context.calcIndex+=1;
				}
			}*/

			//System.out.println("name=" + name + " index=" + regexIndex);
			List<Struct> childern = getChildren();
			if (childern == null) {
				throw new RuntimeException(
						"StructSequenceImpl must have children");
			}
			StringBuffer res = new StringBuffer("(");
			if (getPrefix() != null)
				res.append(quote(getPrefix()));
			for (int i = 0; i < getChildren().size(); i++) {
				if (i + 1 < getChildren().size())
					res.append(getChildren().get(i).calcRegex(context, getDelimiterRegex()));
				else 
					res.append(getChildren().get(i).calcRegex(context, null));

				/*if (getDelimiter() != null && i + 1 < getChildren().size()) {
					res.append(Pattern.quote(getDelimiter()));
				}*/
			}
			if (getSuffix() != null)
				res.append(quote(getSuffix()));
			res.append(")");
			this.regexPure = res.toString();
			res = handleOccur(res, context, delimiter);
			this.regex = res.toString();
		}
		/*if (delimiter != null)
			regex = regex + delimiter;*/
		return regex;

	}

	@Override
	public String getDelimiterRegex() {
		if (delimiterRegex == null)
			return getDelimiterWithLookahead();
		return delimiterRegex;
	}

	private String getDelimiterWithLookahead() {
		if (lookahead == null || "".equals(lookahead)) {
			return delimiter;
		}
		return delimiter + "(?" + (isNegative()?"!":"") + lookahead + ")";
	}
	@Override
	public StructSequence setDelimiterRegex(String del) {
		delimiterRegex = del;
		return this;
	}
	@Override
	public String getLookahead() {
		return lookahead;
	}
	@Override
	public StructSequence setLookahead(String lookahead) {
		if ("".equals(lookahead))
			lookahead = null;
		this.lookahead = lookahead;
		return this;
	}
	@Override
	public boolean isNegative() {
		return isNegative;
	}
	@Override
	public StructSequence setNegative(boolean negative) {

		this.isNegative = negative;
		return this;
	}

}
